package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.qf.mapper.OrderMapper;
import com.qf.myexception.ServiceException;
import com.qf.pojo.Cart;
import com.qf.pojo.Order;
import com.qf.pojo.OrderDetails;
import com.qf.pojo.PatientPojo;
import com.qf.service.CartService;
import com.qf.service.OrderDetailsService;
import com.qf.service.OrderService;
import com.qf.utils.Constant;
import com.qf.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.sql.Array;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    OrderDetailsService orderDetailsService;
    @Autowired
    CartService cartService;

    @Override
    public ResultMsg delByOrderId(String orderId) {

        UpdateWrapper<Order> wrapper = new UpdateWrapper<>();
        wrapper.eq("order_id", orderId);
        wrapper.set("order_delete", 1);
        boolean update = this.update(wrapper);
        if (update) {
            return ResultMsg.SUCCESS();
        }
        throw new ServiceException(201, "删除异常");
    }

    @Override
    public ResultMsg findAll(HttpServletRequest request) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        HttpSession session = request.getSession();
        PatientPojo patient = (PatientPojo) session.getAttribute("patient");
        if (patient == null) {
            throw new ServiceException(201, "用户未登录");
        }
        wrapper.eq("order_delete", Constant.NO_DELETE);
        wrapper.eq("patient_id", patient.getPatientId());
        List<Order> list = this.list(wrapper);
        if (list != null) {
            return ResultMsg.SUCCESS(list);
        }
        throw new ServiceException(201, "查询未登录");
    }

    @Override
    public ResultMsg findOne(String orderId) {
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id", orderId);
        Order one = this.getOne(wrapper);
        if (one != null) {
            List<OrderDetails> list = orderDetailsService.findOne(orderId);
            one.setList(list);
            return ResultMsg.SUCCESS(one);
        }
        throw new ServiceException(201, "查询异常");
    }

    @Override
    public ResultMsg add(ArrayList<Object> list, String remark, HttpServletRequest request) {
        /*
           1.设置订单信息   患者id,总价,生成订单时间,备注,状态,删除
           2.保存订单
           3.批量新增订单详情
         */
        HttpSession session = request.getSession();
        PatientPojo patient = (PatientPojo) session.getAttribute(Constant.PATIENT_SESSION);
        if (patient == null)
            throw new ServiceException(Constant.LZY + 1, "请先登录");
        BigDecimal resultBig = new BigDecimal("0");
        System.out.println("list = " + list);
        for (Object carts : list) {
            LinkedHashMap<Object, Object> cart = (LinkedHashMap<Object, Object>) carts;
            resultBig = resultBig.add(new BigDecimal(cart.get("goodsValue") + ""));
        }
        Order order = new Order(null, patient.getPatientId(), resultBig.floatValue(), new Timestamp(new Date().getTime()), remark, 0, Constant.NO_DELETE,null, null);
        boolean save = this.save(order);
        if (!save)
            return ResultMsg.FAILED(Constant.LZY + 2, "生成订单失败,未知异常");
        ArrayList<OrderDetails> orderDetails = new ArrayList<>();
//        for (Object carts : list) {
//            Cart cart = (Cart) carts;
//            orderDetails.add(new OrderDetails(null, cart.getGoodsId(), order.getOrderId(), cart.getGoodsValue(), Constant.NO_DELETE));
//        }
        for (Object carts : list) {
            LinkedHashMap<Object, Object> cart = (LinkedHashMap<Object, Object>) carts;
            int goodsValueInt = (int) cart.get("goodsValue");
            Float goodsValue = (float) goodsValueInt;
            int goodsPriceInt = (int) cart.get("goodsPrice");
            Float goodsPrice = (float) goodsPriceInt;
            orderDetails.add(new OrderDetails((Integer) null, (Integer) cart.get("goodsId"), order.getOrderId(), goodsValue, Constant.NO_DELETE, (String) cart.get("goodsName"), (Integer) cart.get("goodsNum"), goodsPrice));
        }
        ResultMsg add = orderDetailsService.add(orderDetails);
        if (add.getCode() == 200) {
            for (Object carts : list) {
                LinkedHashMap<Object, Object> cart = (LinkedHashMap<Object, Object>) carts;
                cartService.removeGoods((Integer) cart.get("cartId"));
            }
            return ResultMsg.SUCCESS(order);
        }

        return add;
    }

    @Override
    public ResultMsg updateStatus(String orderId) {
        Order order = this.getOne(new QueryWrapper<Order>().eq("order_id", orderId));
        order.setOrderStatus(1);
        order.setOrderPaytime(new Timestamp(new Date().getTime()));
        boolean b = this.updateById(order);
        if (b)
            return ResultMsg.SUCCESS(order);
        return ResultMsg.FAILED(Constant.LZY + 1, "修改订单状态失败");
    }

}
